package cn.gjxblog.controller;

import cn.gjxblog.api.BlogServerFegin;
import cn.gjxblog.api.CommentServerFegin;
import cn.gjxblog.api.HouseServerFegin;
import cn.gjxblog.common.model.HttpResult;
import cn.gjxblog.common.model.PageCondition;
import cn.gjxblog.common.model.PageData;
import cn.gjxblog.model.dto.BlogQueryDTO;
import cn.gjxblog.model.dto.CommentDTO;
import cn.gjxblog.model.entity.Blog;
import cn.gjxblog.model.entity.Comment;
import cn.gjxblog.model.CommonConstants;
import cn.gjxblog.model.vo.BlogVO;
import cn.gjxblog.model.vo.CommentVO;
import cn.gjxblog.model.vo.HouseVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * 作用:
 * 作者:gjx
 * 版本:V1.0
 * 创建时间: 2019/1/19 18:19
 */
@Controller
public class BlogController {

    @Autowired
    private BlogServerFegin blogServerFegin;

    @Autowired
    private CommentServerFegin commentServerFegin;

    @Autowired
    private HouseServerFegin houseServerFegin;

    @RequestMapping(value="blog/list",method={RequestMethod.POST,RequestMethod.GET})
    public String list(Integer pageSize, Integer pageNum, Blog query, ModelMap modelMap){
        BlogQueryDTO blogQueryDTO = new BlogQueryDTO();
        if(pageSize==null&&pageNum==null){
            pageSize=5;
            pageNum=1;
        }
        blogQueryDTO.setPageCondition(new PageCondition(pageSize,pageNum));
        blogQueryDTO.setBlog(query);
        HttpResult<PageInfo<BlogVO>> pageInfoHttpResult = blogServerFegin.queryBlog(blogQueryDTO);
        PageInfo<BlogVO> result = pageInfoHttpResult.getResult();
        PageData<BlogVO> ps = PageData.buildPage(result.getList(), result.getTotal(), pageSize,pageNum);

        List<HouseVO> houses =  queryHotHouses();
        modelMap.put("recomHouses", houses);
        modelMap.put("ps", ps);
        return "/blog/listing";
    }

    @RequestMapping(value="blog/detail",method={RequestMethod.POST,RequestMethod.GET})
    public String blogDetail(String id,ModelMap modelMap){
        Blog blog = blogServerFegin.getBlog(id).getResult();
        CommentDTO commentDTO = new CommentDTO();
        commentDTO.setBlogId(id);
        commentDTO.setType(2);
        commentDTO.setSize(8);
        List<CommentVO> comments = commentServerFegin.queryComment(commentDTO).getResult();
        List<HouseVO> houses =  queryHotHouses();
        modelMap.put("recomHouses", houses);
        modelMap.put("blog", blog);
        modelMap.put("commentList", comments);
        return "/blog/detail";
    }

    private List<HouseVO> queryHotHouses() {
        HttpResult<List<HouseVO>> restHouses = houseServerFegin.queryHotHouses(CommonConstants.RECOM_SIZE);
        List<HouseVO> rcHouses =restHouses.getResult();
        return  rcHouses;
    }
}
